﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using ComicIsland.Common;
using ComicIsland.Common.Entities;
using ComicIsland.Common.Entities.LocalJson;
using ComicIsland.Common.StorageHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ComicIsland.App.Controls
{
    public partial class SharedControl : UserControl
    {
        public UserThirdLoginInfo UserLoginInfo { get; set; }
        public ShareData shareData { get; set; }

        public event EventHandler OnClose;
        public SharedControl()
        {
            InitializeComponent();
            this.UserLoginInfo = null;
        }

        private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (this.OnClose != null)
            {
                this.OnClose(sender, e);
            }
        }

        private void GetUserLoginInfo()
        {
            if (!IsolatedStorageHelper.Instance.FileExists(KEYS.KEY_UserThirdLoginInfo))
                return;

            string localCacheData = IsolatedStorageHelper.Instance.ReadText(KEYS.KEY_UserThirdLoginInfo);
            if (string.IsNullOrWhiteSpace(localCacheData))
                return;
            this.UserLoginInfo = JSONHelper.Deserialize<UserThirdLoginInfo>(localCacheData);
        }


        private void SinaStackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            GetUserLoginInfo();
            if (!(this.UserLoginInfo != null && this.UserLoginInfo.ComicUserId > 0 && this.UserLoginInfo.ThirdPlatform == (int)ComicIsland.Common.Helper.ThirdPlatform.SinaWeibo))
            {
                MessageToastManager.Show("未登录！");
                return;
            }
            this.shareData.AccessToken = this.UserLoginInfo.AccessToken;
            if (string.IsNullOrWhiteSpace(this.UserLoginInfo.AccessToken))
            {
                MessageToastManager.Show("未登录！");
                return;
            }
            ShareNavigate(ShareType.SinaWeibo);

        }

        private void QQStackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            GetUserLoginInfo();
            if (!(this.UserLoginInfo != null && this.UserLoginInfo.ComicUserId > 0 && this.UserLoginInfo.ThirdPlatform == (int)ComicIsland.Common.Helper.ThirdPlatform.QQ))
            {
                MessageToastManager.Show("未登录！");
                return;
            }
            this.shareData.AccessToken = this.UserLoginInfo.AccessToken;
            this.shareData.OpenId = this.UserLoginInfo.UserId;
            this.shareData.AppKey = App.QQ_AppKey;
            if (string.IsNullOrWhiteSpace(this.shareData.AccessToken) || string.IsNullOrWhiteSpace(this.shareData.OpenId) || string.IsNullOrWhiteSpace(this.shareData.AppKey))
            {
                MessageToastManager.Show("未登录！");
                return;
            }
            ShareNavigate(ShareType.QQWeibo);
        }

        void ShareNavigate(ShareType shareType)
        {
            this.shareData.ShareType = shareType.ToString();
            //
            if (Application.Current.RootVisual is PhoneApplicationFrame)
            {
                string json = JSONHelper.Serialize<ShareData>(this.shareData);
                ((PhoneApplicationFrame)Application.Current.RootVisual).Navigate(new Uri("/Controls/SharedContentPage.xaml?json=" + Uri.EscapeDataString(json), UriKind.Relative));
            }
        }
    }
}
